package com.example.questionnaire.mbg.controller;

import cn.hutool.crypto.SecureUtil;
import com.example.questionnaire.dto.UserAdminDTO;
import com.example.questionnaire.exception.DuplicateException;
import com.example.questionnaire.exception.NotFindException;
import com.example.questionnaire.mbg.comman.R;
import com.example.questionnaire.mbg.entity.User;
import com.example.questionnaire.mbg.service.IUserService;
import com.example.questionnaire.validate.UserValidate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private IUserService userService;

    @PostMapping("/register")
    public R register(@RequestBody UserAdminDTO dto) {
        String username = dto.getUsername();
        String password = dto.getPassword();
        User user = userService.selectUserByUsername(username);

        UserValidate.validateUsername(username);
        UserValidate.validatePassword(password);
        if (null != user) {
            throw new DuplicateException("username");
        }

        password = SecureUtil.md5(password);
        userService.register(username, password);
        return R.success();
    }

    @PostMapping("/login")
    public R login(@RequestBody UserAdminDTO dto) {
        String username = dto.getUsername();
        String password = dto.getPassword();
        User user = userService.selectUserByUsername(username);
        if (null == user) {
            throw new NotFindException("username");
        }
//        if (user.getStatus()) {
//            return R.fail("已登录");
//        }

        if (!SecureUtil.md5(password).equals(user.getPassword())) {
            return R.fail("密码错误");
        }

        userService.login(username);

        return R.success(user);
    }

    @PostMapping("/logout")
    public R logout(@RequestBody UserAdminDTO dto) {
        String username = dto.getUsername();
        User user = userService.selectUserByUsername(username);
        if (null == user) {
            throw new NotFindException("username");
        }
        if (!user.getStatus()) {
            return R.fail("未登录");
        }
        userService.logout(username);
        return R.success();
    }

    @PostMapping("/changePassword")
    public R changePassword(@RequestBody UserAdminDTO dto) {
        String username = dto.getUsername();
        String password = dto.getPassword();
        String newPassword = dto.getNewPassword();
        User user = userService.selectUserByUsername(username);
        if (null == user) {
            throw new NotFindException("username");
        }
        if (!SecureUtil.md5(password).equals(user.getPassword())) {
            return R.fail("密码错误");
        }
        UserValidate.validatePassword(newPassword);
        newPassword = SecureUtil.md5(newPassword);
        userService.changeUserPassword(username, newPassword);
        return R.success();
    }
}